package com.sj.parksdk.fragment.pay;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.ruffian.library.widget.RLinearLayout;
import com.ruffian.library.widget.RTextView;
import com.ruffian.library.widget.helper.RTextViewHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.adapter.base.delegate.SimpleDelegateAdapter;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.net.http.MyNetApi;
import com.sj.parksdk.net.http.callback.TipRequestCallBack;
import com.sj.parksdk.net.request.MGetRequest;
import com.sj.parksdk.net.result.databean.Data_PackageRecord;
import com.sj.parksdk.setting.Dictionary;
import com.sj.parksdk.utils.RefreshState;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.statelayout.StatefulLayout;
import com.xuexiang.xutil.common.StringUtils;

import butterknife.BindView;

@Page(name = "套餐购买记录")
public class FragmentPackageRecord extends BaseFragment {
    @BindView(R2.id.recycler)
    RecyclerView recycler;
    @BindView(R2.id.ll_state)
    StatefulLayout llState;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    private SimpleDelegateAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_layout_common_refresh_list;
    }

    @Override
    protected void initViews() {
        adapter = new SimpleDelegateAdapter<Data_PackageRecord>(R.layout.parksdk_adapter_package_record, new LinearLayoutHelper()) {
            @Override
            protected void bindData(@NonNull RecyclerViewHolder holder, int position, Data_PackageRecord item) {
                holder.text(R.id.tv_car_number, item.getCarNo());
                RTextView rtvCarColor = holder.findViewById(R.id.rtv_car_color);
                RTextViewHelper rtvCarColorHeler = rtvCarColor.getHelper();
                RLinearLayout rllCarColor = holder.findViewById(R.id.rll_car_color);
                int carColor = ResUtils.getColor(Dictionary.Car.PlateColor.PalteColorMap.get(StringUtils.isEmpty(item.getPlateColor()) ? Dictionary.Car.PlateColor.Unknow : item.getPlateColor()));
                rtvCarColorHeler.setBackgroundColorNormal(carColor);
                rtvCarColorHeler.setTextColorNormal(ResUtils.getColor(Dictionary.Car.PlateColor.White.equals(item.getPlateColor()) ? R.color.color_3 : R.color.color_white));
                rtvCarColorHeler.setBorderWidthNormal(DensityUtils.dp2px(Dictionary.Car.PlateColor.White.equals(item.getPlateColor()) ? 1 : 0));
                rtvCarColor.setText(Dictionary.Car.PlateColor.PlateColorNameMap.get(item.getPlateColor()));
                rllCarColor.getHelper().setBorderColorNormal(Dictionary.Car.PlateColor.White.equals(item.getPlateColor()) ?ResUtils.getColor(R.color.color_3):carColor);

                holder.text(R.id.tv_package_content, item.getPackageContent());
                holder.text(R.id.tv_pay_content, item.getPayContent());
            }
        };
        WidgetUtils.initRecyclerView(recycler, DensityUtils.dp2px(10), ResUtils.getColor(R.color.color_cut_line));
        recycler.setAdapter(adapter);
    }

    @Override
    protected void initListeners() {
        refresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageIndex++;
                queryPackageRecord(RefreshState.more);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                pageIndex = 1;
                queryPackageRecord(RefreshState.refresh);
            }
        });
    }

    private int pageIndex = 1;

    private void queryPackageRecord(int flag) {
        new MGetRequest(MyNetApi.PACKAGE_ORDER_RECORD)
                .user()
                .params(MyNetApi.PageParam.pageIndex, pageIndex)
                .params(MyNetApi.PageParam.pageSize, 10)
                .execute(new TipRequestCallBack<Data_PackageRecord>() {
                    @Override
                    public void onSuccess(Data_PackageRecord response) throws Throwable {
                        queryPackageRecordSuccess(response, flag);
                    }

                    @Override
                    public void onError(ApiException e) {
                        queryPackageRecordError(e.getMessage(),flag);
                    }
                });
    }

    private void queryPackageRecordError(String message,int flag) {
        llState.showError(message,null);
        switch (flag){
            case RefreshState.refresh:
                refresh.finishRefresh();
                break;
            case RefreshState.more:
                refresh.finishLoadMore(false);
                break;

        }
    }

    private void queryPackageRecordSuccess(Data_PackageRecord data, int flag) {
        if (data == null) {
            if(flag == RefreshState.refresh)llState.showCustom(emptyStatus());
            return;
        }
        switch (flag) {
            case RefreshState.refresh:
                if (data.getItems().size() == 0) llState.showCustom(emptyStatus());
                else llState.showContent();
                adapter.refresh(data.getItems());
                refresh.finishRefresh();
                if (data.getTotal() == data.getItems().size()) {
                    refresh.setNoMoreData(true);
                }
                break;
            case RefreshState.more:
                adapter.loadMore(data.getItems());
                if (data.getTotal() == adapter.getData().size()) {
                    refresh.finishLoadMoreWithNoMoreData();
                    break;
                }
                refresh.finishLoadMore(true);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        refresh.autoRefresh();
    }
}
